package com.banciyuan.bcywebview.biz.circles.discover;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.view.dialog.CircleFilterDialog;
import com.banciyuan.bcywebview.biz.circles.discover.tagnav.DiscoverTagNavFragment;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;

public class DiscoverCircleActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvCircleFilter;
    private View headerSplitLine;
    private List<Fragment> mList = new ArrayList<>();
    public final static int TYPE_TAG = 0;
    public final static int TYPE_WORK = 1;
    private int default_type = TYPE_TAG;
    private String tag_type;
    private CircleFilterDialog circleFilterDialog;
    private String[] actionTiltes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discovercircle_layout);
        initArgs();
        initUi();
        initDialog();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        default_type = getIntent().getIntExtra(gotoUtil.INTENT_VALUE_ONE, TYPE_TAG);
        tag_type = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_TWO);
        actionTiltes = new String[]{getString(R.string.discover_tag_circle), getString(R.string.discover_work_circle)};
        initDiscoverTag();
        initDiscoverWork();
    }

    private void initDiscoverWork() {
        DiscoverWorkFragment discoverWorkFragment = new DiscoverWorkFragment();
        Bundle bundleWork = new Bundle();
        bundleWork.putInt(gotoUtil.INTENT_VALUE_ONE, DiscoverWorkFragment.TYPE_PRODUCTION);
        discoverWorkFragment.setArguments(bundleWork);
        mList.add(discoverWorkFragment);
    }

    private void initDiscoverTag() {
        DiscoverTagNavFragment discoverTagFragment = new DiscoverTagNavFragment();
        if (!TextUtils.isEmpty(tag_type)) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("tag_type", tag_type);
            discoverTagFragment.setArguments(bundle);
        }
        mList.add(discoverTagFragment);
    }

    @Override
    protected void initUi() {
        tvCircleFilter = (TextView) findViewById(R.id.tv_circle_filter);
        headerSplitLine = findViewById(R.id.view_header_split_line);
    }

    private void initDialog() {
        CircleFilterDialog.Builder builder = new CircleFilterDialog.Builder(this);
        circleFilterDialog = builder.setDefault_type(default_type).setTagClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePage(0);
            }
        }).setWorkClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePage(1);
            }
        }).create();
        circleFilterDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                tvCircleFilter.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.arrow_down_pink, 0);
            }
        });
        circleFilterDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                tvCircleFilter.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.arrow_up_pink, 0);
            }
        });
    }

    private void changePage(int pageNum) {
        headerSplitLine.setVisibility(pageNum == 0 ? View.VISIBLE : View.GONE);
        showFragment(pageNum);
        tvCircleFilter.setText(actionTiltes[pageNum]);
    }

    @Override
    protected void initAction() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        tvCircleFilter.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        changePage(default_type);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_circle_filter:
                showDialog();
                break;
        }
    }

    private void showDialog() {
        circleFilterDialog.show();
    }

    private void showFragment(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : mList) {
            ft.hide(fragment);
        }
        Fragment fragment = mList.get(position);
        if (!fragment.isAdded())
            ft.add(R.id.fl_circle_layout, fragment, fragment.getClass().getSimpleName());
        ft.show(fragment);
        ft.commit();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }
}
